#include<iomanip>
#include<iostream>
#include<string>
#include<list>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::list;
using std::ostream;

class CompareStudent;
class Student
{
public:
    Student(const char* name, int age,float chinese,float math,float english)
        :_name(name)
         ,_age(age)
         ,_chinese(chinese)
         ,_math(math)
         ,_english(english)
    {
        cout<<"  STudent init  "<<endl;
    }
    friend
        ostream& operator<<(ostream& ost,const Student& rhs);

    float get_sums() const
    {
        return _chinese+_math+_english; 
    }


    friend class CompareStudent;

private:
    string _name;
    int _age;
    float _chinese;
    float _math;
    float _english;
};
ostream& operator<<(ostream& ost,const Student& rhs)
{
    ost<<rhs._name<<std::setw(5)<<rhs._age<<std::setw(12)<<rhs._chinese<<std::setw(12)<<rhs._math<<std::setw(12)<<rhs._english
        <<std::setw(5)<<rhs._math+rhs._english+rhs._chinese;
    return ost;
}
class CompareStudent
{
public:
    bool operator()(const Student& lhs,const Student& rhs)
    {
        float sum1 =lhs._chinese+lhs._math+lhs._english;
        float sum2 =rhs._chinese+rhs._math+rhs._english;
        cout<<"sum1-sum2  "<<sum1<< " - "<<sum2<<endl;
        if(sum1!=sum2)
        {
            return sum1>sum2;
        }
        if(lhs._chinese!=rhs._chinese)
        {
            return lhs._chinese>rhs._chinese;
        }
        if(lhs._math!=rhs._math)
        {
            return lhs._math>rhs._math;
        }
            return lhs._english>rhs._english;
    }
};

int main(void)
{
    /* list<Student> sys={{"oo",19,90,80,89},{"kk",20,69,78,89},{"lli",17,100,100,100},{"ll",18,80,90,99}}; */
    /* list<Student> sys={{"kk",20,69,78,89},{"lli",17,100,100,100},{"ll",18,80,90,99},{"oo",19,90,80,89}}; */
    list<Student> sys={{"ll",18,80,90,99},{"oo",19,90,80,89},{"kk",20,69,78,89},{"lli",17,100,100,100}};
    for(auto& i:sys)
    {
        cout<<i<<endl;
    }
    cout<<endl<<endl;
    sys.sort(CompareStudent());
    cout<<endl;
    cout<<"name"<<std::setw(5)<<"age"<<std::setw(12)<<"chinese"<<std::setw(12)<<"math"<<std::setw(12)<<"english"
        <<std::setw(5)<<"all"<<endl;;
    for(auto& i:sys)
    {
        cout<<i<<endl;
    }
}

